/*
 * Copyright 2007-2008 Sun Microsystems, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Sun designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Sun in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 */

package jsr203.nio.file;

/**
 * Represents an event or a repeated event for an object that is registered
 * with a {@link WatchService}.
 *
 * <p> An event has a {@link #getType() type} and a {@link #getCount() count} to
 * indicate the number of times that the event has been observed. This allows
 * for efficient representation of repeated events. The {@link #getContext()
 * getContext} method returns any context associated with the event. In the case
 * of a repeated event then the context is the same for all events.
 *
 * @param   <T>     The type of the context object associated with the event
 *
 * @since 1.7
 */

public abstract class WatchEvent<T> {
    /**
     *  Initializes a new instance of this class.
     */
    protected WatchEvent() { }

    /**
     * An event type, for the purposes of identification, or a <em>flag</em>
     * that qualifies how a {@link Watchable} is registered with a {@link
     * WatchService}.
     *
     * @since 1.7
     */
    public static interface Type {
        /**
         * Returns the name of the event type or flag.
         */
        String name();
    }

    /**
     * Returns the event type.
     *
     * @return  The event type
     */
    public abstract Type getType();

    /**
     * Returns the event count. If the event count is greater than {@code 1}
     * then this is a repeated event.
     *
     * @return  The event count
     */
    public abstract int getCount();

    /**
     * Returns the context for the event.
     *
     * <p> In the case of {@link StandardWatchEventType#ENTRY_CREATE ENTRY_CREATE},
     * {@link StandardWatchEventType#ENTRY_DELETE ENTRY_DELETE}, and {@link
     * StandardWatchEventType#ENTRY_MODIFY ENTRY_MODIFY} events the context is
     * a {@code Path} that has a single name component that is the name of the
     * file in the directory.
     *
     * @return  The event context; may be {@code null}
     */
    public abstract T getContext();

    /**
     * <em>Casts</em> this event to an event with {@link #getContext context}
     * of the given type. This method simply checks that this event's context
     * is of the given type.
     * <pre>
     *     WatchEvent&lt;?&gt; event = ...
     *     if (event.getType() == StandardWatchEventType.ENTRY_MODIFY) {
     *         WatchEvent&lt;Path&gt; modEvent = event.cast(Path.class);
     *     }
     * </pre>
     *
     * @param   type
     *          The type of the context object
     *
     * @return  This watch event
     *
     * @throws  ClassCastException
     *          If the context object is not of the given type
     */
    @SuppressWarnings("unchecked")
    public final <T> WatchEvent<T> cast(Class<T> type) {
        Object ob = getContext();
        if (ob != null && !type.isInstance(ob))
            throw new ClassCastException();
        return (WatchEvent<T>)this;
    }
}
